package com.kgc.web;

import com.kgc.dao.AdminCarDao;
import com.kgc.entity.Car;
import com.kgc.entity.UserCar;
import com.kgc.untils.PageUtils;
import com.kgc.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "CarServlet", value = "/CarServlet")
public class CarServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opt = request.getParameter("opt");
        AdminCarDao adminCarDao = new AdminCarDao();
        PrintWriter writer = response.getWriter();
        PageUtils pageUtils = new PageUtils();
        if ("list".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String name = request.getParameter("name");
            int totalDanYuan = adminCarDao.totalCar(name);
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(totalDanYuan);
            List<Car> danYuanList = adminCarDao.selectCar(pageUtils, name);
            writer.write(R.success("数据加载成功", totalDanYuan, danYuanList).toJSON());
        } else if ("add".equals(opt)) {
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            int i = adminCarDao.addCar(new Car(name, Integer.parseInt(type), 0));
            if (i > 0) {
                writer.write(R.success("添加成功!").toJSON());
            } else {
                writer.write(R.error("添加失败!").toJSON());
            }
        } else if ("delete".equals(opt)) {
            String id = request.getParameter("id");
            int count = adminCarDao.deleteCar(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        } else if ("edit".equals(opt)) {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            String status = request.getParameter("status");
            int count = adminCarDao.updateCar(new Car(Integer.parseInt(id), name, Integer.parseInt(type), Integer.parseInt(status)));
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败").toJSON());
            }
        } else if ("uerCarList".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String id = request.getParameter("id");
            int totalUserCar = adminCarDao.totaluserCar(Integer.parseInt(id));
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            List<UserCar> list = adminCarDao.selectUserCar(pageUtils, Integer.parseInt(id));
            pageUtils.init(totalUserCar);
            writer.write(R.success("数据加载成功", totalUserCar, list).toJSON());
        } else if ("type".equals(opt)) {  // 查询空闲车位
            int type = Integer.parseInt(request.getParameter("type"));
            List<Car> carList = adminCarDao.selectFreeCar(type);
            writer.write(R.success("车位列表加载成功..", carList).toJSON());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
